package com.example.himalaya.adapters;

import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.RecyclerView;

import com.example.himalaya.R;
import com.example.himalaya.base.BaseApplication;
import com.example.himalaya.utils.LogUtils;
import com.example.himalaya.views.SobPopWindow;
import com.ximalaya.ting.android.opensdk.model.track.Track;

import java.util.ArrayList;
import java.util.List;

public class PlayListAdapter extends RecyclerView.Adapter<PlayListAdapter.InnerHolder> {

    private static final String TAG = "PlayListAdapter";
    private List<Track> mData = new ArrayList<>();
    private View mPlayingIconView;
    private int playingIndex = 0;
    private SobPopWindow.PlayListItemClickListener  mItemClickListener = null;

    @NonNull
    @Override
    public InnerHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        View mItemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_play_list, parent, false);
        return new InnerHolder(mItemView);
    }



    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    public void onBindViewHolder(@NonNull InnerHolder holder, final int position) {
       //holder被点击
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mItemClickListener != null) {
                    mItemClickListener.onItemClick(position);
                }
            }
        });

        //设置数据
        Track track = mData.get(position);
        TextView trackTitle = holder.itemView.findViewById(R.id.track_title);
        //设置播放的标题字体颜色
        trackTitle.setTextColor(
                BaseApplication.getAppContext().getColor(playingIndex == position ? R.color.mainColor : R.color.sub_text_title));
        trackTitle.setText(track.getTrackTitle());
        //找播放图标
        LogUtils.d(TAG, "position   " + position + "playindex" + playingIndex);
        mPlayingIconView = holder.itemView.findViewById(R.id.play_icon_iv);
        mPlayingIconView.setVisibility(playingIndex == position ? View.VISIBLE : View.GONE);

    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    public void setData(List<Track> data) {
        //设置数据更新列表
        mData.clear();
        mData.addAll(data);
        notifyDataSetChanged();

    }

    public void setCurrentPlayPosition(int index) {
        playingIndex =index;
        notifyDataSetChanged();
    }

    public void setOnItemClickListener(SobPopWindow.PlayListItemClickListener listener) {
        this.mItemClickListener =listener;
    }

    public class InnerHolder extends RecyclerView.ViewHolder {
        public InnerHolder(@NonNull View itemView) {
            super(itemView);
        }
    }
}
